#include "ControlPoint.h"

#include <iostream>

using namespace std;

ControlPoint::ControlPoint()
{

}

ControlPoint::~ControlPoint()
{

}

int ControlPoint::callbackEventHandler(Upnp_EventType eventType, void *event, void *clientd)
{
	cout << "ZZZZZZZZZZZZZZZZZZZZZZ" << endl;
	return 0;
}

void *ControlPoint::timerLoop()
{
	cout << "BBBBBBBBBBBBBBB" << endl;
	while(1)
		sleep(1000);

	cout << "CCCCCCCCCCCCCC" << endl;
}

int ControlPoint::refresh()
{
	int rc;

	devices->removeAll();

	rc = UpnpSearchAsync(controlHandle, 5, "sddp:all", NULL);

	return 0;
}

int ControlPoint::start()
{
	pthread_t timer_thread;
	int rc;
	unsigned short port = 0;
	char *ip_address = NULL;

	devices = new DeviceList();

	rc = UpnpInit(ip_address, port);
	
	if (!ip_address) {
		ip_address = UpnpGetServerIpAddress();
	}
	if (!port) {
		port = UpnpGetServerPort();
	}

	rc = UpnpRegisterClient(&ControlPoint::callbackEventHandlerProxy, this, &controlHandle);

	refresh();

	rc = pthread_create(&timerThread, NULL, ControlPoint::timerLoopProxy, this);
	
	pthread_exit(NULL);

	cout << "AAAAAAAAAAAAAAAAA" << endl;
	return rc;
}
